package com.yu1996.gulimall.coupon.service.impl;

import com.yu1996.common.to.MemberPrice;
import com.yu1996.common.to.SkuReductionTo;
import com.yu1996.gulimall.coupon.entity.MemberPriceEntity;
import com.yu1996.gulimall.coupon.entity.SkuLadderEntity;
import com.yu1996.gulimall.coupon.service.MemberPriceService;
import com.yu1996.gulimall.coupon.service.SkuLadderService;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.coupon.dao.SkuFullReductionDao;
import com.yu1996.gulimall.coupon.entity.SkuFullReductionEntity;
import com.yu1996.gulimall.coupon.service.SkuFullReductionService;

@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl
    extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity>
    implements SkuFullReductionService {

  @Autowired private SkuLadderService skuLadderService;
  @Autowired private MemberPriceService memberPriceService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<SkuFullReductionEntity> page =
        this.page(
            new Query<SkuFullReductionEntity>().getPage(params),
            new QueryWrapper<SkuFullReductionEntity>());

    return new PageUtils(page);
  }

  /**
   * 保存商品添加时的商品满减信息
   *
   * @param skuReductionTo
   */
  @Override
  public void saveSkuReduction(SkuReductionTo skuReductionTo) {

    // 满减打折：sms_sku_ladder
    SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
    BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
    skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
    if (skuLadderEntity.getFullCount() > 0) {
      skuLadderService.save(skuLadderEntity);
    }

    // sms_sku_full_reduction
    SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
    BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
    if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {

      this.save(skuFullReductionEntity);
    }
    // sms_member_price
    List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
    if (memberPrice != null && memberPrice.size() > 0) {
      List<MemberPriceEntity> collect =
          memberPrice.stream()
              .map(
                  item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                  })
              .filter(
                  item1 -> {
                    return item1.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
                  })
              .collect(Collectors.toList());
      memberPriceService.saveBatch(collect);
    }
  }
}
